package it.enricod.jcontextfree.engine.model.impl;

import it.enricod.jcontextfree.engine.model.IFigureParameters;
import it.enricod.jcontextfree.engine.model.IFigureRegionCalculator;
import it.enricod.jcontextfree.engine.model.FigureParameters;
import it.enricod.jcontextfree.engine.model.FigureRegion;
import it.enricod.jcontextfree.engine.model.vo.Position2D;

import static java.lang.Math.*;

/**
 * spazio occupato da un quadrato
 * 
 * @author enrico
 * 
 */
public class SquareRegionCalculator implements IFigureRegionCalculator {

    /**
     * 
     */
    public FigureRegion calculateRegion(IFigureParameters parameters) {
        double s = parameters.getAbsolute().getScale();
        // half size
        double hs = s / 2;

        double r = parameters.getAbsolute().getRotation();

        double posR = abs(r);
        if (posR > Math.PI / 2) {
            posR = posR % (PI / 2);
        }

        double delta = abs(hs * sqrt(2) * cos( abs(PI/4-posR)));

        // (x1,y1) = punto in basso a sinistra
        // (x2,y2) = punto in alto a destra
        double x1, y1, x2, y2;

        x1 = parameters.getAbsolute().getX() - delta;
        y1 = parameters.getAbsolute().getY() - delta;

        x2 = parameters.getAbsolute().getX() + delta;
        y2 = parameters.getAbsolute().getY() + delta;

        return new FigureRegion(Position2D.createInstance(x1, y1), Position2D.createInstance(x2, y2));
    }
}
